import { Sendable } from '../../models/Sendable';
import { TaskBuilder } from '../../models/TaskBuilder';
import { htmlToElement } from '../../utils/dom';
import { Parser } from '../Parser';
export class SYZOJProblemParser extends Parser{
    public getMatchPatterns(): string[] {
        return [];
    }
    public async parse(url: string, html: string): Promise<Sendable> {
        const elem = htmlToElement(html);
        const task = new TaskBuilder('SYZOJ').setUrl(url);
        this.parseFromPage(task,elem);
        return task.build();
    }
    private parseFromPage(task : TaskBuilder,elem: Element): void {
        task.setName(elem.querySelector("h1.ui.header").textContent.trim());
        const timeLimitStr = elem.querySelector("div.ui.center.aligned.grid> div:nth-child(2) > span:nth-child(2)").textContent.substring(5,4);
        task.setTimeLimit(parseFloat(timeLimitStr) * 1000);
        
        const memoryLimitStr = elem.querySelector("div.ui.center.aligned.grid> div:nth-child(2) > span:nth-child(2)").textContent.substring(5,4);
        task.setMemoryLimit(parseFloat(memoryLimitStr) * 1000);

        const datas = elem.querySelectorAll("div.ui.existing.segment");
        for(var i = 0;i < datas.length;i += 2){
            var inStr = datas[i].querySelector("code").textContent;
            var outStr = datas[i + 1].querySelector("code").textContent;
            task.addTest(inStr,outStr);
        }
    }
}